package com.hns.can.service.service;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.entity.BaseDrvChkParamInfoEntity;
import com.hns.can.service.vo.AbnEmotionScoreVo;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DataUtil;
import com.hns.tool.pub.PubUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 情绪考核设置-评分设置Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class AbnEmotionScoreService extends CommonService {

    /**
     * @param paraMap
     * @return
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-11-12
     * @功能说明：情绪异常评分标准配置 - 查询情绪异常评分标准配置信息(该机构下没有相应配置就取默认的)
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<BaseDrvChkParamInfoEntity> searchAbnEmotionScoreInfo(Map<String, Object> paraMap) {
        List<BaseDrvChkParamInfoEntity> list = PubUtil.convertListMapToObject(
                this.sqlMapDao.selectList("baseDrvChkParamInfo.searchBaseDrvChkParamInfo", paraMap), BaseDrvChkParamInfoEntity.class);
        return list;
    }

    /**
     * @param data
     * @throws Exception
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-11-12
     * @功能说明：情绪异常评分标准配置 - 修改情绪异常评分标准配置信息
     */
    @Transactional
    public void uptAbnEmotionScoreInfo(AbnEmotionScoreVo data) throws Exception {
        String seleId = data.getSeleId();
        String currId = data.getCurrId();
        List<String> chkIds = data.getChkIds();
        List<BigDecimal> aeScoreWVs = data.getAeScoreWVs();
        List<BigDecimal> aeDedPoints = data.getAeDedPoints();
        List<BigDecimal> aeScoreACs = data.getAeScoreACs();
        SysUserEntity user = this.getCurrentUser().getUser();
        String corpId = data.getCorpId();
        if (seleId.equals(currId)) { // 更新已有的数据
            for(int i = 0; i < chkIds.size(); i++){
                BaseDrvChkParamInfoEntity entity = this.commonEntityDao.find(chkIds.get(i), BaseDrvChkParamInfoEntity.class);
                entity.setWeightValue(aeScoreWVs.get(i));
                entity.setDeductValue(aeDedPoints.get(i));
                entity.setAllowCount(aeScoreACs.get(i));
                entity.setUpdateTime(new Date());
                entity.setUpdateUser(user.getAcctName());
                this.commonEntityDao.update(entity);
            }
        } else { // 添加新数据
            for(int i = 0; i < chkIds.size(); i++){
                BaseDrvChkParamInfoEntity entity = this.commonEntityDao.find(chkIds.get(i), BaseDrvChkParamInfoEntity.class);
                BaseDrvChkParamInfoEntity dbentity = new BaseDrvChkParamInfoEntity();
                dbentity.setChkId(DataUtil.sequenceID());
                PubUtil.copyPropertiesInclude(entity, dbentity, new String[]{"organLineType","chkCgyCode","chkCgyName","chkCode","chkName","param","allowUnit"});
                dbentity.setWeightValue(aeScoreWVs.get(i));
                dbentity.setDeductValue(aeDedPoints.get(i));
                dbentity.setAllowCount(aeScoreACs.get(i));
                dbentity.setOrganLineId(seleId);
                dbentity.setCrtTime(new Date());
                dbentity.setCrtUser(user.getAcctName());
                dbentity.setUpdateTime(new Date());
                dbentity.setUpdateUser(user.getAcctName());
                dbentity.setCorpId(corpId);
                this.commonEntityDao.save(dbentity);
            }
        }
    }
}